import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/home.vue'
import { sessionCache } from '@/utils/cache'
import { start, close } from '../utils/nprogress'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/home/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        meta: {
          title: '系统首页',
          requirePermission: false,
        },
        component: () => import(/* webpackChunkName: "dashboard" */ '../views/system/DashBoard.vue'),
      },
      {
        path: 'overview',
        name: 'overview',
        meta: {
          title: '系统概述',
          requirePermission: false,
        },
        component: () => import(/* webpackChunkName: "overview" */ '../views/system/OverView.vue'),
      },
      {
        path: 'article',
        name: 'article',
        meta: {
          title: '发布文章',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "Article" */ '../views/article/Article.vue'),
      },
      {
        path: 'article_list',
        name: 'article_list',
        meta: {
          title: '文章列表 ',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "table" */ '../views/article/ArticleList.vue'),
      },
      {
        path: 'article_edit/:id',
        name: 'article_edit',
        meta: {
          title: '修改文章',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "table" */ '../views/article/ArticleEdit.vue'),
      },
      {
        path: 'label',
        name: 'label',
        meta: {
          title: '标签列表',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "label" */ '../views/label/Index.vue'),
      },
      {
        path: 'type',
        name: 'type',
        meta: {
          title: '类型列表',
        },
        component: () => import(/* webpackChunkName: "type" */ '../views/type/Index.vue'),
      },
      {
        path: 'comment_article',
        name: 'comment_article',
        meta: {
          title: '文章评论',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "comment_article" */ '../views/comment/CommentArticle.vue'),
      },
      {
        path: 'comment_message',
        name: 'comment_message',
        meta: {
          title: '说说评论',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "comment_message" */ '../views/comment/CommentMessage.vue'),
      },
      {
        path: 'create_message',
        name: 'create_message',
        meta: {
          title: '发布说说',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "create_message" */ '../views/message/MessageEditor.vue'),
      },
      {
        path: 'message_list',
        name: 'message_list',
        meta: {
          title: '说说列表',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "message_list" */ '../views/message/MessageList.vue'),
      },
      {
        path: 'create_word',
        name: 'create_word',
        meta: {
          title: '发布留言',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "create_word" */ '../views/word/WordEdit.vue'),
      },
      {
        path: 'word_list',
        name: 'word_list',
        meta: {
          title: '留言列表',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "word_list" */ '../views/word/WordList.vue'),
      },
      {
        path: 'page_rights_controller',
        name: 'page_rights_controller',
        meta: {
          title: '页面权限',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "rights_controller" */ '../views/rights/Page.vue'),
      },
      {
        path: 'interface_rights_controller',
        name: 'interface_rights_controller',
        meta: {
          title: '接口权限',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "interface_rights_controller" */ '../views/rights/Interface.vue'),
      },
      {
        path: 'role',
        name: 'role',
        meta: {
          title: '角色列表',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "role" */ '../views/rights/Role.vue'),
      },
      {
        path: 'profile',
        name: 'profile',
        meta: {
          title: '个人中心',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "profile" */ '../views/user/Profile.vue'),
      },
      {
        path: 'users',
        name: 'users',
        meta: {
          title: '用户列表',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "users" */ '../views/user/UsersList.vue'),
      },
      {
        path: 'visitors',
        name: 'visitors',
        meta: {
          title: '访客列表',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "visitors" */ '../views/user/VisitorsList.vue'),
      },
      {
        path: 'export',
        name: 'export',
        meta: {
          title: '导出Excel',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "export" */ '../views/export.vue'),
      },
      {
        path: 'import',
        name: 'import',
        meta: {
          title: '导入Excel',
          requirePermission: true,
        },
        component: () => import(/* webpackChunkName: "import" */ '../views/import.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录',
    },
    component: () => import(/* webpackChunkName: "login" */ '@/views/login/Index.vue'),
  },
  {
    path: '/403',
    name: '403',
    meta: {
      title: '没有权限',
    },
    component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
  },
  {
    path: '/:catchAll(.*)',
    name: '404',
    component: () => import(/* webpackChunkName: "404" */ '../views/404.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 进度条
router.beforeEach((pre, next) => {
  start()
})
router.afterEach(() => {
  close()
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if (sessionCache.getCache('userInfo')) {
      // 已经登录
      if (to.meta.requirePermission) {
        const roleMenu = sessionCache.getCache('roleMenu')
        if (roleMenu.includes(to.name)) {
          next()
        } else {
          next('/403')
        }
      } else {
        next()
      }
    } else {
      // 未登录
      next('/login')
    }
  }
})

export default router
